import {
    INIT_USER,
    USER_DATA,
    LOGIN_OUT

} from './mutations-types.js';


export default{
    state:{
        token:null,          //token
        userInfo:{},        //用户信息
        userStatus:false    //登录状态
    },
    getters:{},
    mutations:{
        [USER_DATA]( state ){
            if(   localStorage.getItem('userInfo')   ){
                let userObj = JSON.parse( localStorage.getItem('userInfo') );
                
                state.token = userObj.token;//存储用户token了
                state.userInfo = userObj;//存储用户信息了
                state.userStatus = true;//登录了
            }
        },
        [INIT_USER](  state  , userObj   ){
            state.token = userObj.token;//存储用户token了
            state.userInfo = userObj;//存储用户信息了
            state.userStatus = true;//登录了
            //存到本地存储
            localStorage.setItem('userInfo',JSON.stringify(userObj));
        },
        //退出登录
        [LOGIN_OUT]( state ){
            state.token = null;
            state.userInfo = {};
            state.userStatus = false;
            //清空本地存储
            localStorage.removeItem('userInfo');
        }
    },
    actions:{}
}